package com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.configs.classifyconfing.MyClassifyConfing;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyentity.MyClassifyDetailEntity;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyentity.MyClassifyDetailItemActivity;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import org.w3c.dom.Text;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class ClassifyDetailItem extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener {
    private PullToRefreshGridView gridView;
    private String id;
    private String textTitle;
    //分页
    private int page=1;
    private int newPage=1;
    private int newsestPage=1;
    private int scalesParams=2;
    private int downPrice=3;
    private int upPrice=4;
    //设置点击事件的标记
    private int titlePostion;
    private List<MyClassifyDetailEntity.DataBean.ListBean> listDatas;
    private MyClassifyDetailAdapter detailAdapter;
    private ImageView imageView_back;
    private ImageView detail_newest;
    private ImageView detail_sales;
    private ImageView detail_choosePrice;
    private TextView text_detail_item;
    //查找textview
    private TextView text_news;
    private TextView text_sales;
    private TextView text_price;
    private TextView text_moren;
    //设置标记来记录点击价格的次数
    private boolean flag;
    //设置标记来记录滑动事件
    private boolean scoll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify__detail__item);
        initView();
        initData();
        //下拉刷新的监听
        setListener();
        //滑动监听
        setScolllistener();
    }

    private void initData() {
        Intent intent = getIntent();
        id=intent.getStringExtra("id");
        textTitle=intent.getStringExtra("textTitle");
        text_detail_item.setText(textTitle);
        listDatas=new ArrayList<>();
        detailAdapter=new MyClassifyDetailAdapter(listDatas,this);
        getDetail(page);
        gridView.setAdapter(detailAdapter);
        text_moren.setTextColor(Color.BLUE);
        gridView.setOnItemClickListener(this);
    }

    private void initView() {
        text_detail_item= (TextView) this.findViewById(R.id.text_detail_title);
        gridView= (PullToRefreshGridView) this.findViewById(R.id.detail_gridView);
        imageView_back= (ImageView) this.findViewById(R.id.image_back);
        imageView_back.setOnClickListener(this);
        detail_newest= (ImageView) this.findViewById(R.id.detail_newest);
        detail_newest.setOnClickListener(this);
        detail_sales= (ImageView) this.findViewById(R.id.detail_sales);
        detail_sales.setOnClickListener(this);
        detail_choosePrice= (ImageView) this.findViewById(R.id.detail_chooseprice);
        detail_choosePrice.setOnClickListener(this);
        text_moren= (TextView) this.findViewById(R.id.text_moren);
        text_moren.setOnClickListener(this);
        text_news= (TextView) this.findViewById(R.id.text_newe);
        text_sales= (TextView) this.findViewById(R.id.text_sale);
        text_price= (TextView) this.findViewById(R.id.text_price);
    }

    public void getDetail(int page){
        String path= MyClassifyConfing.Details_first+id+MyClassifyConfing.Details_pageFirst+"0"+MyClassifyConfing.Details_pageMiddle+page+MyClassifyConfing.Details_pageEnd;
        RequestParams params=new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MyClassifyDetailEntity myClassifyDetail = gson.fromJson(result, MyClassifyDetailEntity.class);
                List<MyClassifyDetailEntity.DataBean.ListBean> list = myClassifyDetail.getData().getList();
                listDatas.addAll(list);
                detailAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(ClassifyDetailItem.this, "网络访问不成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_back:
                finish();
                break;
            case R.id.detail_newest:
                //设置点击事件标记
                titlePostion=1;
                //设置图标颜色
                detail_newest.setImageResource(R.drawable.cell_list_down_s);
                detail_sales.setImageResource(R.drawable.cell_list_down_n);
                detail_choosePrice.setImageResource(R.drawable.cell_list_price_off);
                //设置字体颜色
                text_news.setTextColor(Color.BLUE);
                text_price.setTextColor(Color.GRAY);
                text_sales.setTextColor(Color.GRAY);
                text_moren.setTextColor(Color.GRAY);
                listDatas.clear();
                newPage=1;
                getNew(newsestPage, newPage);
               // Toast.makeText(ClassifyDetailItem.this, "我走到了这一步了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.detail_sales:

               titlePostion=2;
                //设置图标颜色
                detail_newest.setImageResource(R.drawable.cell_list_down_n);
                detail_sales.setImageResource(R.drawable.cell_list_down_s);
                detail_choosePrice.setImageResource(R.drawable.cell_list_price_off);
                //设置字体颜色
                text_news.setTextColor(Color.GRAY);
                text_price.setTextColor(Color.GRAY);
                text_sales.setTextColor(Color.BLUE);
                text_moren.setTextColor(Color.GRAY);
                listDatas.clear();
                newPage=1;
                getNew(scalesParams, newPage);
               // Toast.makeText(ClassifyDetailItem.this, "我走到了这一步了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.detail_chooseprice:
                //设置图标颜色
                detail_newest.setImageResource(R.drawable.cell_list_down_n);
                detail_sales.setImageResource(R.drawable.cell_list_down_n);
                if(!flag) {
                    detail_choosePrice.setImageResource(R.drawable.cell_list_price_down);
                    titlePostion=3;
                    listDatas.clear();
                    newPage=1;
                    getNew(downPrice, newPage);
                    flag=true;
                }else if(flag){
                    detail_choosePrice.setImageResource(R.drawable.cell_list_price_up);
                    titlePostion=4;
                    listDatas.clear();
                    newPage=1;
                    getNew(upPrice,newPage);
                    flag=false;
                }
                //设置字体颜色
                text_news.setTextColor(Color.GRAY);
                text_price.setTextColor(Color.BLUE);
                text_sales.setTextColor(Color.GRAY);
                text_moren.setTextColor(Color.GRAY);
                break;
            case R.id.text_moren:
                newPage=1;
                //设置点击事件的标记
                titlePostion=0;
                //设置图标颜色
                detail_newest.setImageResource(R.drawable.cell_list_down_n);
                detail_sales.setImageResource(R.drawable.cell_list_down_n);
                detail_choosePrice.setImageResource(R.drawable.cell_list_price_off);
                //设置字体颜色
                text_news.setTextColor(Color.GRAY);
                text_price.setTextColor(Color.GRAY);
                text_sales.setTextColor(Color.GRAY);
                text_moren.setTextColor(Color.BLUE);
                listDatas.clear();
                page=1;
                getDetail(page);
                break;
        }
    }
    //设置下拉和下拉刷新：
    public void setListener(){
        gridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                listDatas.clear();
                //默认数据的下拉刷新
                if(titlePostion==0) {
                    page = 1;
                    getDetail(page);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            gridView.onRefreshComplete();
                        }
                    }, 1000);
                }
                //最新数据的下拉刷新
                if(titlePostion==1){
                    newPage=1;
                    getNew(newsestPage, newPage);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            gridView.onRefreshComplete();
                        }
                    }, 1000);
                }
                //销量的下拉刷新
                if(titlePostion==2){
                    newPage=1;
                    getNew(scalesParams,newPage);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            gridView.onRefreshComplete();
                        }
                    }, 1000);
                }
                //价格下降的下拉刷新
                if(titlePostion==3){
                    newPage=1;
                    getNew(downPrice,newPage);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            gridView.onRefreshComplete();
                        }
                    }, 1000);
                }
                //价格上升的下拉刷新
                if(titlePostion==4){
                    newPage=1;
                    getNew(upPrice,newPage);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            gridView.onRefreshComplete();
                        }
                    }, 1000);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {

            }
        });
    }
    public void setScolllistener(){
        gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //利用点击标记来判断刷新哪一个事件：此事件为默认事件
                if (scoll && scrollState == SCROLL_STATE_IDLE&&titlePostion==0) {
                    page++;
                    getDetail(page);
                }
                //利用点击标记来判断刷新哪一个事件：此事件为最新事件
                if (scoll && scrollState == SCROLL_STATE_IDLE&&titlePostion==1) {
                    newPage++;
                    getNew(newsestPage,newPage);
                }
                //利用点击标记来判断刷新哪一个事件：此事件为销量事件
                if (scoll && scrollState == SCROLL_STATE_IDLE&&titlePostion==2) {
                    newPage++;
                    getNew(scalesParams,newPage);
                }
                //利用点击标记来判断刷新哪一个事件：此事件为价格下降事件
                if (scoll && scrollState == SCROLL_STATE_IDLE&&titlePostion==3) {
                    newPage++;
                    getNew(downPrice,newPage);
                }
                //利用点击标记来判断刷新哪一个事件：此事件为价格上升事件
                if (scoll && scrollState == SCROLL_STATE_IDLE&&titlePostion==4) {
                    newPage++;
                    getNew(upPrice,newPage);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    scoll = true;
                }
            }
        });
    }
    //最新数据
    public void getNew(int paramer,int newPage){
        String path= MyClassifyConfing.Details_first+id+MyClassifyConfing.Details_pageFirst+paramer+MyClassifyConfing.Details_pageMiddle+newPage+MyClassifyConfing.Details_pageEnd;
        RequestParams params=new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                MyClassifyDetailEntity myClassifyDetail = gson.fromJson(result, MyClassifyDetailEntity.class);
                List<MyClassifyDetailEntity.DataBean.ListBean> list = myClassifyDetail.getData().getList();
                listDatas.addAll(list);
                detailAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(ClassifyDetailItem.this, "网络访问失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       TextView textId= (TextView) view.findViewById(R.id.detail_id);
        String DetailId=textId.getText().toString();
        Intent intent=new Intent();
        intent.putExtra("id",DetailId);
        intent.setClass(this, MyClassifyDetailItemActivity.class);
        startActivity(intent);
    }
}
